<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <p>选择上传图片：<input type="file" id="file" accept="image/*" /></p>
    <button id="btn">上传图片</button>
    <img src="" alt="">
    <script>
      const imgInput = document.querySelector("input[type=file]");
      const btn = document.querySelector("#btn");
      const img = document.querySelector("img");
      btn.addEventListener("click", () => {
        const file = imgInput.files[0];
        const formData = new FormData();
        formData.append("file", file, file.name);
        fetch("/api/upload", {
          method: "POST",
          body: formData,
        })
          .then((res) => res.json())
          .then((res) => {
            console.log(res);
            img.src = res.data.url
          });
      });
    </script>
  </body>
</html>
